/**
 * <pre>
 * @(#)DataTable.java 1.00 2009-11-9
 * 
 * Copyright (c) 1998, 2009 Regular Individual. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 *     Haixu Huang
 * </pre>
 **/
package cn.sh.huang.chart.data;

import java.util.ArrayList;
import java.util.List;

import cn.sh.huang.chart.ext.map.DoubleKeyedMap;

/**
 * <pre>
 * DataTable ...
 * 
 * @version 1.0
 * </pre>
 */
public class DataTable
{
    private final Argument[] arguments;
    private final List seriesList = new ArrayList();
    private final DoubleKeyedMap dataMap = new DoubleKeyedMap();
    private transient DataSeries[] seriez = null;

    public DataTable(Argument[] args)
    {
        if (args == null || args.length == 0) {
            throw new IllegalArgumentException("!ERROR! Empty arguments.");
        }
        for (int i = 0; i < args.length; i++) {
            if (args[i] == null) {
                throw new IllegalArgumentException("!ERROR! Illegal argument(s) found.");
            }
        }
        this.arguments = args;
    }

    public void addSeries(DataSeries ser, Object[] data)
    {
        if (data == null || data.length != arguments.length) {
            throw new IllegalArgumentException("!ERROR! Illegal variables declared.");
        }
        if (seriesList.contains(ser)) {
            throw new IllegalArgumentException("!ERROR! Duplicated series.");
        }
        seriesList.add(ser);
        for (int i = 0; i < arguments.length; i++) {
            dataMap.put(ser, arguments[i], new Variable(data[i]));
            ser.setRange(data[i]);
        }
    }

    public Variable getVariable(DataSeries ser, Argument arg)
    {
        return (Variable) dataMap.get(ser, arg)[0].getValue();
    }

    public Argument[] getArguments()
    {
        return arguments;
    }

    public DataSeries[] getSeriez()
    {
        if (seriez == null || seriez.length != seriesList.size()) {
            seriez = (DataSeries[]) seriesList.toArray(new DataSeries[seriesList.size()]);
        }
        return seriez;
    }
}
